package com.dream.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.base.controller.BaseController;
import com.dream.master.service.ProductService;
import com.dream.master.service.ShopService;
import com.dream.model.master.Product;
import com.dream.model.order.OnlineOrder;
import com.dream.model.order.OnlineOrder.OrderStatus;
import com.dream.order.service.OrderService;
import com.dream.utils.annotation.AccessLog;
import com.dream.utils.annotation.SkipAuthority;
import com.dream.utils.hibernate.PagingVO;
import com.dream.utils.log.DreamLogUtils;
import com.dream.vo.api.ApiOrder;
import com.dream.vo.api.ApiOrderUpdate;
import com.dream.vo.api.ApiResult;
import com.dream.vo.order.OrderQueryVO;
import com.google.common.collect.Lists;
import com.google.gson.Gson;

/**
 * 
 * @author EvanYu
 *
 */
@Controller
@RequestMapping(value = "/api/order")
public class ApiOrderController extends BaseController{
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private ProductService productService;
	
	
	@ResponseBody
	@RequestMapping(value = "/list",produces = "text/json;charset=UTF-8")
	@AccessLog(module="api",content="我的订单")
	@SkipAuthority
	public String list(Model modelMap,OrderQueryVO vo,HttpServletResponse response) {
		
		ApiResult result = new ApiResult();
		List<ApiOrder> data = Lists.newArrayList();
		List<String> shops = shopService.queryMyShopId(getUser());
		if(CollectionUtils.isNotEmpty(shops)){
			vo.setShops(shops);
			vo.setStatus(OrderStatus.order_accept.name());
			PagingVO query = orderService.query(vo, PagingVO.initMaxSize(), getUser());
			@SuppressWarnings("unchecked")
			List<OnlineOrder> details = (List<OnlineOrder>)query.getDetails();
			if(CollectionUtils.isNotEmpty(details)){
				for(OnlineOrder order : details){
					data.add(order.toApiOrder());
				}
			}
		}
		result.setMessage(new Gson().toJson(data));
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value = "/detail",produces = "text/json;charset=UTF-8")
	@AccessLog(module="api",content="订单详细")
	@SkipAuthority
	public String detail(Model modelMap,Long id,HttpServletResponse response) {
		
		ApiResult result = new ApiResult();
		
		OnlineOrder detail = orderService.getDetail(id);
		if(detail != null){
			result.setMessage(new Gson().toJson(detail.toApiOrderDetail()));
		}
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value = "/update",produces = "text/json;charset=UTF-8")
	@AccessLog(module="api",content="订单更新")
	@SkipAuthority
	public String update(Model modelMap,String gsonOrder,HttpServletResponse response) {
		
		ApiResult result = new ApiResult();
		try{
			
			ApiOrderUpdate order = ApiOrderUpdate.fromGson(gsonOrder);
			Product p = productService.search(order.getProName(), Long.parseLong(order.getProSecondCatCode()));
			if(p == null){
				result.setResult(false);
				result.setMessage("当前机型不存在!");
			} else{
				order.setProductId(p.getId());
				orderService.updateOrder(order, getUser());
				result.setResult(true);
			}
		}catch(Exception ex){
			DreamLogUtils.error(this,"update","update order occured exception", ex);
			result.setResult(false);
		}
		return new Gson().toJson(result);
	}
	
	
	
}
